<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page.sync="page"
               v-model="form"
               ref="crud"
               @search-change="searchChange"
               @search-reset="searchReset"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @sort-change="sortChange"
               @on-load="onLoad">
      <template slot-scope="{row}" slot="menu">
        <el-button size="small" type="text" icon="el-icon-view" @click="detail(row)">
          被攻击详情
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList} from "@/youan/api/attacked/attackedAnalysis";
  import {toLine} from "../../../util/common";

  export default {
    data() {
      return {
        form: {},
        query: {
          isAttackedSuccess: "",
          attackedIp: ""
        },
        loading: true,
        page: {
          pageSize: 20,
          currentPage: 1,
          total: 0
        },
        currentOrder: {},
        option: {
          height:'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          delBtn:false,
          editBtn: false,
          addBtn: false,
          dialogType: "drawer",
          dialogClickModal: false,
          menuWidth: 160,
          column: [
            {
              label: "",
              prop: "id",
              hide: true
            },
            {
              label: "日期",
              prop: "date",
              search: true,
              type: "date",
              valueFormat: "yyyy-MM-dd"
            },
            {
              label: "被攻击主机IP",
              prop: "attackedIp",
              search: true,
              searchLabelWidth: 120
            },
            {
              label: "被攻击次数",
              prop: "attackedTotalCount",
              sortable: "custom",
            },
            {
              label: "被攻击成功次数",
              prop: "attackedSuccessCount",
              sortable: "custom",
              width: 160
            },
            {
              label: "是否被攻击成功",
              prop: "isAttackedSuccess",
              type: "select",
              dicData: [
                {
                  label: "成功",
                  value: "1"
                },
                {
                  label: "失败",
                  value: "2"
                }
              ],
              display: false,
              hide: true,
              search: true,
              searchLabelWidth: 120,
              clearable: true
            },
            {
              label: "登陆成功数",
              prop: "loginSuccessCount",
              sortable: "custom",
            },
            {
              label: "登陆失败数",
              prop: "loginFailureCount",
              sortable: "custom",
            },
            {
              label: "攻击函数",
              prop: "attackFunctionCount",
              sortable: "custom",
            },
            {
              label: "国家",
              prop: "country",
            },
            {
              label: "省份",
              prop: "province",
            },
            {
              label: "城市",
              prop: "city",
            },
            {
              label: "网络供应商",
              prop: "isp",
            },
          ]
        },
        data: []
      };
    },
    activated() {
      this.$nextTick(()=>{
        if(this.$refs.crud.$refs.table){
          this.$refs.crud.$refs.table.doLayout();
        }
      })
    },
    methods: {
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        const query = {};
        if(this.query.isAttackedSuccess) {
          if(this.query.isAttackedSuccess === "1") {
            query.attacked_success_count_gt = 0;
          }
          if(this.query.isAttackedSuccess === "2") {
            query.attacked_success_count_equal = 0;
          }
        }

        if(this.currentOrder.descs) {
          query.descs = this.currentOrder.descs
        }

        if(this.currentOrder.ascs) {
          query.ascs = this.currentOrder.ascs
        }

        if(this.query.attackedIp) {
          query.attackedIp = this.query.attackedIp;
        }
        if(this.query.date) {
          query.date = this.query.date;
        }

        this.loading = true;
        getList(page.currentPage, page.pageSize, query).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
        });
      },
      sortChange({prop, order}) {
        if(order == "descending") {
          this.currentOrder = {descs: toLine(prop)};
          this.onLoad(this.page)
        }else if(order == "ascending") {
          this.currentOrder = {descs: toLine(prop)};
          this.onLoad(this.page)
        }else {
          this.currentOrder = {};
          this.onLoad(this.page)
        }
      },
      detail(row) {
        this.$router.push({
          path: "/youan/audit/search/attackAndAttacked",
          query: row
        });
      }
    },
    activated() {
      this.$nextTick(() => {
        if (this.$refs.crud.$refs.table) {
          this.$refs.crud.$refs.table.doLayout();
        }
      })
    },
  };
</script>

<style>
</style>
